package com.scmt.hmzd.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.scmt.base.utils.BASE64DecodedMultipartFile;
import com.scmt.hmzd.service.IHmProjectService;

import javax.servlet.http.HttpServletResponse;

import com.scmt.hmzd.utils.UploadFileUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import com.scmt.core.common.utils.ResultUtil;
import com.scmt.core.common.vo.PageVo;
import com.scmt.core.common.vo.Result;
import com.scmt.core.common.vo.SearchVo;
import com.scmt.core.common.utils.SecurityUtil;
import com.scmt.hmzd.entity.HmProject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author
 **/
@RestController
@Api(tags = " 项目管理数据接口")
@RequestMapping("/scmt/hmProject")
public class HmProjectController {
    @Autowired
    private IHmProjectService hmProjectService;

    /**
     * 功能描述：新增项目管理数据
     *
     * @param hmProject 实体
     * @return 返回新增结果
     */
    @ApiOperation("新增项目管理数据")
    @PostMapping("addHmProject")
    public Result<Object> addHmProject(@RequestBody HmProject hmProject) {
        try {
            hmProject.setDelFlag(0);
            hmProject.setCreateTime(new Date());
            if (StringUtils.isNotBlank(hmProject.getCover())) {
                MultipartFile imgFile = BASE64DecodedMultipartFile.base64ToMultipart(hmProject.getCover());
                String fileUrl = UploadFileUtils.uploadFile(imgFile);
                hmProject.setCover(fileUrl);
            }
            boolean res = hmProjectService.save(hmProject);
            if (res) {
                return ResultUtil.data(res, "保存成功");
            } else {
                return ResultUtil.data(res, "保存失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error("保存异常:" + e.getMessage());
        }
    }

    /**
     * 功能描述：更新数据
     *
     * @param hmProject 实体
     * @return 返回更新结果
     */
    @ApiOperation("更新项目管理数据")
    @PostMapping("updateHmProject")
    public Result<Object> updateHmProject(@RequestBody HmProject hmProject) {
        if (StringUtils.isBlank(hmProject.getId())) {
            return ResultUtil.error("参数为空，请联系管理员！！");
        }
        try {
            //是否有路径
            if (!StringUtils.isBlank(hmProject.getCover()) && hmProject.getCover().indexOf(";base64") > -1) {
                //base64 转文件
                MultipartFile imgFile = BASE64DecodedMultipartFile.base64ToMultipart(hmProject.getCover());
                String fileUrl = UploadFileUtils.uploadFile(imgFile);
                hmProject.setCover(fileUrl);
            }

            hmProject.setUpdateTime(new Date());
            boolean res = hmProjectService.updateById(hmProject);
            if (res) {
                return ResultUtil.data(res, "修改成功");
            } else {
                return ResultUtil.data(res, "修改失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error("保存异常:" + e.getMessage());
        }
    }

    /**
     * 功能描述：根据主键来删除数据
     *
     * @param ids 主键集合
     * @return 返回删除结果
     */
    @ApiOperation("根据主键来删除项目管理数据")
    @PostMapping("deleteHmProject")
    public Result<Object> deleteHmProject(@RequestParam String[] ids) {
        if (ids == null || ids.length == 0) {
            return ResultUtil.error("参数为空，请联系管理员！！");
        }
        try {
            boolean res = hmProjectService.removeByIds(Arrays.asList(ids));
            if (res) {
                return ResultUtil.data(res, "删除成功");
            } else {
                return ResultUtil.data(res, "删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error("删除异常:" + e.getMessage());
        }
    }

    /**
     * 功能描述：根据主键来获取数据
     *
     * @param id 主键
     * @return 返回获取结果
     */
    @ApiOperation("根据主键来获取项目管理数据")
    @GetMapping("getHmProject")
    public Result<Object> getHmProject(@RequestParam(name = "id") String id) {
        if (StringUtils.isBlank(id)) {
            return ResultUtil.error("参数为空，请联系管理员！！");
        }
        try {
            HmProject res = hmProjectService.getById(id);
            if (res != null) {
                return ResultUtil.data(res, "查询成功");
            } else {
                return ResultUtil.data(res, "查询失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error("查询异常:" + e.getMessage());
        }
    }

    /**
     * 功能描述：实现分页查询
     *
     * @param searchVo 需要模糊查询的信息
     * @param pageVo   分页参数
     * @return 返回获取结果
     */
    @ApiOperation("分页查询项目管理数据")
    @GetMapping("queryHmProjectList")
    public Result<Object> queryHmProjectList(HmProject hmProject, SearchVo searchVo, PageVo pageVo) {
        try {
            IPage<HmProject> result = hmProjectService.queryHmProjectListByPage(hmProject, searchVo, pageVo);
            return ResultUtil.data(result);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error("查询异常:" + e.getMessage());
        }
    }

    @ApiOperation("分页查询项目管理数据")
    @GetMapping("queryAllHmProjectList")
    public Result<Object> queryAllHmProjectList(HmProject hmProject) {
        try {
            List<HmProject> result = hmProjectService.queryAllHmProjectList(hmProject);
            return ResultUtil.data(result);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error("查询异常:" + e.getMessage());
        }
    }

    /**
     * 功能描述：导出数据
     *
     * @param response  请求参数
     * @param hmProject 查询参数
     * @return
     */
    @ApiOperation("导出项目管理数据")
    @PostMapping("/download")
    public void download(HttpServletResponse response, HmProject hmProject) {
        try {
            hmProjectService.download(hmProject, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
